package br.com.manutencaocarro.helper;

import java.util.Date;

import android.widget.DatePicker;
import android.widget.Spinner;
import br.com.manutencaocarro.R;
import br.com.manutencaocarro.activity.RelatorioActivity;
import br.com.manutencaocarro.modelo.Carro;
import br.com.manutencaocarro.modelo.Relatorio;

public class RelatorioHelper {

	private DatePicker dataInicialRelatorio;
	private DatePicker dataFinalRelatorio;
	private Spinner listaCarros;

	public RelatorioHelper(RelatorioActivity relatorioActivity) {
		dataInicialRelatorio = (DatePicker) relatorioActivity.findViewById(R.id.dataInicialRel);
		dataFinalRelatorio = (DatePicker) relatorioActivity.findViewById(R.id.dataFinalRel);
		listaCarros = (Spinner) relatorioActivity.findViewById(R.id.lista_carros_rel);
	}
	
	@SuppressWarnings("deprecation")
	public boolean validaRelatorio()
	{
		int inicialDay = dataInicialRelatorio.getDayOfMonth();
		int inicialMonth = dataInicialRelatorio.getMonth();
		int inicialYear = dataInicialRelatorio.getYear();
		Date inicialDate = new Date(inicialYear-1900, inicialMonth, inicialDay);
		
		int finalDay = dataFinalRelatorio.getDayOfMonth();
		int finalMonth = dataFinalRelatorio.getMonth();
		int finalYear = dataFinalRelatorio.getYear();
		Date finalDate = new Date(finalYear-1900, finalMonth, finalDay);
		
		if( inicialDate.compareTo(finalDate) <= 0 )
		{
			return true;
		}
		
		return false;
	}
	
	@SuppressWarnings("deprecation")
	public Relatorio pegaDadosRelatorio()
	{
		Relatorio relatorio = new Relatorio();
		int inicialDay = dataInicialRelatorio.getDayOfMonth();
		int inicialMonth = dataInicialRelatorio.getMonth();
		int inicialYear = dataInicialRelatorio.getYear();
		Date inicialDate = new Date(inicialYear - 1900, inicialMonth, inicialDay);
		relatorio.setDataInicio( inicialDate );
		
		int finalDay = dataFinalRelatorio.getDayOfMonth();
		int finalMonth = dataFinalRelatorio.getMonth();
		int finalYear = dataFinalRelatorio.getYear();
		Date finalDate = new Date(finalYear - 1900, finalMonth, finalDay);
		relatorio.setDataFim(finalDate);
		Carro carro = (Carro) listaCarros.getSelectedItem();
		relatorio.setIdCarro(carro.getId());
		
		return relatorio;
	}
}